home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / tail1.lqr / TAIL.C next >
Text File  |  1985-06-29  |  4KB  |  178 lines

  1. /* Subject: Aztec C source - tail.c */
  2. /* tail - print last part of file */
  3.  
  4. /* Changes, which work for Lattice C.                              */
  5. /* Moved cbuf inside tail(), which puts it on the runtime stack    */
  6. /* Increased the runtime stack to its maximum, 32767 bytes         */
  7. /* Increased BUFSIZE to all but 2048 bytes of the run time stack   */
  8. /* The result of all this is that much bigger files will fit into  */
  9. /* the buffer intact, and you can extract a bigger tail.  At the   */
  10. /* same time, there is no storage in the TAIL.COM for the buffer,  */
  11. /* so it is smaller than the output of the original file.          */
  12. /* TAIL.COM could undoubtedly be shrunk by another 4000 bytes by   */
  13. /* going to level I IO and TINY_MAIN.                              */
  14. /* Lew Paper, 6/29/85                                              */
  15.  
  16. #include "stdio.h"
  17. #define TRUE 1
  18. #define FALSE 0
  19. #define BLANK ' '
  20. #define NL 0x0a
  21. #define TAB 0x09
  22.  
  23. int lines, chars ;
  24.  
  25. main(argc, argv)
  26. int argc ;
  27. char *argv[] ;
  28. {
  29.     char *s ;
  30.     FILE *input ;
  31.     int count ;
  32.  
  33.  
  34.     argc-- ; argv++ ;
  35.     lines = TRUE ;
  36.     chars = FALSE ;
  37.     count = -10 ;
  38.  
  39.     if( argc == 0 ) {
  40.         tail( stdin, count ) ;
  41.         exit(0) ;
  42.     }
  43.  
  44.     s = *argv ;
  45.     if( *s == '-' || *s == '+' ) {
  46.         s++ ;
  47.         if( *s >= '0' && *s <= '9' ) {
  48.             count = stoi( *argv ) ;
  49.             s++ ;
  50.             while( *s >= '0' && *s <= '9' )
  51.                 s++ ;
  52.         }
  53.         if( *s == 'c' ) {
  54.             chars = TRUE ;
  55.             lines = FALSE ;
  56.         }
  57.         else if( *s != 'l' && *s != EOS ) {
  58.             fprintf(stderr, "tail: unknown option %c\n", *s ) ;
  59.             argc = 0 ;
  60.         }
  61.         argc-- ; argv++ ;
  62.     }
  63.  
  64.     if( argc < 0 ) {
  65.         fprintf(stderr, "usage: tail [+/-[number][lc]] [files]\n");
  66.         exit(1) ;
  67.     }
  68.  
  69.     if( argc == 0 )
  70.         tail( stdin, count ) ;
  71.  
  72.     else if( (input=fopen(*argv,"r")) == NULL ) {
  73.         fprintf(stderr, "tail: can't open %s\n", *argv) ;
  74.         exit(1) ;
  75.     }
  76.     else {
  77.         tail( input, count ) ;
  78.         fclose( input ) ;
  79.     }
  80.  
  81.     exit(0) ;
  82.  
  83. } /* end main */
  84.  
  85. /* stoi - convert string to integer */
  86.  
  87. stoi(s)
  88. char *s ;
  89. {
  90.     int n, sign ;
  91.  
  92.     while( *s == BLANK || *s == NL || *s == TAB )
  93.         s++ ;
  94.  
  95.     sign = 1 ;
  96.     if( *s == '+' )
  97.         s++ ;
  98.     else if( *s == '-' ) {
  99.         sign = -1 ;
  100.         s++ ;
  101.     }
  102.     for( n=0 ; *s >= '0' && *s <= '9' ; s++ )
  103.         n = 10 * n + *s - '0' ;
  104.     return( sign * n ) ;
  105. }
  106.  
  107. /* tail - print 'count' lines/chars */
  108.  
  109. #define INCR(p)  if(p >= end) p=cbuf ; else p++
  110. #define BUFSIZE 30719 /* 32767 - 2048 */
  111.  
  112. int _stack = BUFSIZE + 2048;
  113.  
  114. tail( in, goal )
  115. FILE *in ;
  116. int goal ;
  117. {
  118.     int c, count ;
  119.     char *start, *finish, *end ;
  120.     char cbuf[ BUFSIZE ] ;
  121.  
  122.     count = 0 ;
  123.  
  124.     if( goal > 0 ) {    /* skip */
  125.  
  126.         if( lines )     /* lines */
  127.             while( (c=getc(in)) != EOF ) {
  128.                 if( c == NL )
  129.                     count++ ; 
  130.                 if( count >= goal )
  131.                     break ;
  132.             }
  133.         else            /* chars */
  134.             while( getc(in) != EOF ) {
  135.                 count++ ;
  136.                 if( count >= goal )
  137.                     break ;
  138.             }
  139.         if( count >= goal )
  140.             while( (c=getc(in)) != EOF )
  141.                 putc(c, stdout ) ;
  142.     }
  143.  
  144.     else {              /* tail */
  145.  
  146.         goal = -goal ;
  147.         start = finish = cbuf ;
  148.         end = &cbuf[ BUFSIZE - 1 ] ;
  149.  
  150.         while( (c=getc(in)) != EOF ) {
  151.             *finish = c ;
  152.             INCR( finish ) ;
  153.  
  154.             if( start == finish )
  155.                 INCR( start ) ;
  156.             if( !lines || c == NL )
  157.                 count++ ;
  158.  
  159.             if( count > goal ) {
  160.                 count = goal ;
  161.                 if( lines )
  162.                     while( *start != NL )
  163.                         INCR( start ) ;
  164.                 INCR( start ) ;
  165.             }
  166.  
  167.         } /* end while */
  168.  
  169.         while( start != finish ) {
  170.             putc( *start, stdout ) ;
  171.             INCR( start ) ;
  172.         }
  173.  
  174.     } /* end else */
  175.  
  176. } /* end tail */
  177.  
  178.